package com.cai310.lottery.ticket.protocol.local.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.cai310.lottery.Constant;
import com.cai310.lottery.common.Lottery;
import com.cai310.lottery.common.SalesMode;
import com.cai310.lottery.common.WinningUpdateStatus;
import com.cai310.lottery.entity.lottery.ticket.Ticket;
import com.cai310.lottery.entity.lottery.zc.SfzcPasscount;
import com.cai310.lottery.entity.lottery.zc.SfzcPeriodData;
import com.cai310.lottery.entity.lottery.zc.SfzcScheme;
import com.cai310.lottery.exception.DataException;
import com.cai310.lottery.prizeutils.template.VariableString;
import com.cai310.lottery.service.ServiceException;
import com.cai310.lottery.support.PrizeItem;
import com.cai310.lottery.support.zc.PlayType;
import com.cai310.lottery.support.zc.SchemeConverWork;
import com.cai310.lottery.support.zc.SfzcCompoundItem;
import com.cai310.lottery.support.zc.ZcCompoundItem;
import com.cai310.lottery.support.zc.ZcCompoundPassWork;
import com.cai310.lottery.support.zc.ZcPrizeWork;
import com.cai310.lottery.support.zc.ZcUtils;
import com.cai310.lottery.utils.BigDecimalUtil;

public  class SFZCCPUtil extends CPUtil{
	/**
	 *  
	 * @param lottery
	 * @param playType
	 * @return
	 */
	private static final String SINGSPILTCODE = "+";
	private static final String COMPOUNDSPILTCODE = ",";
	@Override
	public String getCpLotteryId(Lottery lottery,Byte betType){
		if(null==lottery)return null;
		if(Lottery.SFZC.equals(lottery)){
			return "3";
		}
		return null;
	}
	@Override
	public String getCpBetType(Ticket ticket){
		if(Lottery.SFZC.equals(ticket.getLotteryType())){
			if(Byte.valueOf("0").equals(ticket.getBetType())){
				return "0";
				//14
			}else if(Byte.valueOf("1").equals(ticket.getBetType())){
				return "1";
				//9
			}
		}
		return "404";
	}
	@Override
	public String getCpPlayType(Ticket ticket){
		if(null==ticket.getMode())return null;
		if(SalesMode.SINGLE.equals(ticket.getMode())){
			return "1";
		}else if(SalesMode.COMPOUND.equals(ticket.getMode())){
			return "0";
		}
		return null;
	}
	@Override
	public String getBetContent(Ticket ticket){
		if(SalesMode.SINGLE.equals(ticket.getMode())){
			return ticket.getContent();
		}else if(SalesMode.COMPOUND.equals(ticket.getMode())){
			return ticket.getContent();
		}
		return null;
	}
	@Override
	public String getSpecialFlag(Ticket ticket) {
		return "";
	}
	@Override
	public String getUpdatePeriodNumber(Ticket ticket) {
		return null;
	}
	public static ZcPrizeWork getZcPrizeWorkByTicket(Ticket ticket, SfzcPeriodData periodData) throws DataException{
		if(null==ticket.getMode())return null;
		if(SalesMode.SINGLE.equals(ticket.getMode())){
			return updateSingleResult(ticket,periodData);
		}else if(SalesMode.COMPOUND.equals(ticket.getMode())){
			return updateCompoundResult(ticket,periodData);
		}
		return null;
	}
	public static SfzcCompoundItem[] getCompoundContent(Ticket ticket) throws DataException {
		if (StringUtils.isBlank(ticket.getContent())) {
			throw new DataException("出票内容获取为空.");
		}
		String[] itemStrs = null;
		PlayType playType = PlayType.values()[ticket.getBetType()];
		if (playType == PlayType.SFZC9) {
			itemStrs = ticket.getContent().split(COMPOUNDSPILTCODE);
		} else {
			itemStrs = ticket.getContent().split(COMPOUNDSPILTCODE);
		}
		if (itemStrs.length != ZcUtils.getMatchCount(Lottery.SFZC)) {
			throw new DataException("方案内容选项个数异常.");
		}
		SfzcCompoundItem[] items = new SfzcCompoundItem[itemStrs.length];
		for (int i = 0; i < itemStrs.length; i++) {
			items[i] = new SfzcCompoundItem(itemStrs[i], i);
		}
		return items;
	}
	protected static ZcPrizeWork updateCompoundResult(Ticket ticket, SfzcPeriodData periodData) throws DataException {

		SfzcCompoundItem[] betItems = getCompoundContent(ticket);
		String result = periodData.getResult();// 开奖结果

		if (betItems.length != result.length())
			throw new DataException("投注结果与开奖结果个数不符.");

		List<ZcCompoundItem> danList = new ArrayList<ZcCompoundItem>();
		List<ZcCompoundItem> unDanList = new ArrayList<ZcCompoundItem>();
		for (SfzcCompoundItem item : betItems) {
			if (item.selectedCount() > 0) {
				if (item.isShedan()) {
					danList.add(item);
				} else {
					unDanList.add(item);
				}
			}
		}

		ZcCompoundPassWork passWork = null;
		PlayType playType = PlayType.values()[ticket.getBetType()];
		if (PlayType.SFZC9.equals(playType)) {// 九场
			SchemeConverWork<ZcCompoundItem> work = new SchemeConverWork<ZcCompoundItem>(
					ZcUtils.SFZC9_MATCH_MINSELECT_COUNT, danList, unDanList, -1, -1);//拆单后都没有设胆
			passWork = new ZcCompoundPassWork(work.getResultList(), ticket.getMultiple(), result);

		} else {
			passWork = new ZcCompoundPassWork(betItems, ticket.getMultiple(), result);
		}

		ZcPrizeWork prizeWork = new ZcPrizeWork(periodData, playType, passWork.getLost0_count(), passWork
				.getLost1_count(), passWork.getLost2_count(), passWork.getLost3_count());

		return prizeWork;
	}
	protected static ZcPrizeWork updateSingleResult(Ticket ticket, SfzcPeriodData periodData) throws DataException {
		String content = ticket.getContent();
		if(content.indexOf(SINGSPILTCODE)!=-1){
			content = content.replaceAll("\\"+SINGSPILTCODE, "\r\n");
		}
		BufferedReader reader = new BufferedReader(new StringReader(content));
		String result = periodData.getResult();// 开奖结果

		int lost0 = 0;
		int lost1 = 0;
		int lost2 = 0;
		int lost3 = 0;
		int line_count = 0;
		int passcount = 0;
		StringBuffer content0 = new StringBuffer();
		StringBuffer content1 = new StringBuffer();
		StringBuffer content2 = new StringBuffer();
		StringBuffer content3 = new StringBuffer();
		boolean finalResult = true;
		PlayType playType = PlayType.values()[ticket.getBetType()];
		try {
			String line = reader.readLine();
			int passcountTemp=0;
			while (line != null) {
				if (StringUtils.isNotBlank(line)) {
					if (line.length() != ZcUtils.getMatchCount(Lottery.SFZC)
							&& line.length() != result.length())
						throw new DataException("单式选项不符");
					int lost_count = 0;
					int selected_count = 0;
					char bet, rs_bet;// 投注内容及结果存放变量
					for (int i = 0; i < line.length(); i++) {
						bet = line.charAt(i);
						rs_bet = result.charAt(i);
						if (Character.isDigit(bet)) {
							selected_count++;
						}
						if (Character.isDigit(rs_bet)) {
							if (Character.isDigit(bet) && bet != rs_bet) {
								lost_count++;
							}
						} else {
							finalResult = false;
						}
					}
					if (lost_count == 0) {
						lost0++;
						content0.append(line).append("\r\n");
					}
					if (lost_count == 1) {
						lost1++;
						content1.append(line).append("\r\n");
					}
					if (lost_count == 2) {
						lost2++;
						content2.append(line).append("\r\n");
					}
					if (lost_count == 3) {
						lost3++;
						content3.append(line).append("\r\n");
					}
					if (PlayType.SFZC9.equals(playType)) {// 九场
						passcountTemp=9-lost_count;
					}else {
						passcountTemp=14-lost_count;
					}
					if(passcountTemp>passcount){
						passcount=passcountTemp;
					}
					line_count++;
				}
				line = reader.readLine();
			}

			int multiple = ticket.getMultiple();
            lost0=lost0 * multiple;
            lost1=lost1 * multiple;
            lost2=lost2 * multiple;
            lost3=lost3 * multiple;
            
			if (line_count * ticket.getMultiple() * 2 != ticket.getSchemeCost().intValue())
				throw new DataException("方案内容与投注金额不符.");

		} catch (IOException e) {
			e.printStackTrace();
		}
		ZcPrizeWork prizeWork = new ZcPrizeWork(periodData, playType, lost0, lost1, lost2, lost3);
		return prizeWork;
	}
	
}
